package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Registlevel;
import com.foreknow.service.RegistlevelService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//挂号等级管理
@RestController
@RequestMapping("/registlevel")
public class RegistlevelController {

    @Autowired
    private RegistlevelService registlevelService;

    //根据id查询挂号级别信息 参数 id
    @RequestMapping(value = "/findRegistlevelById",method = RequestMethod.POST)
    public String findRegistlevelById(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        Integer id = (Integer) para.get("id");
        Registlevel registlevel = registlevelService.selectByPrimaryKey(id);
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();
        //判断是否查询成功
        if(registlevel!=null){
            hs.put("meta",new Constance("msg",200));
            hs.put("registlevel",registlevel);
        }else{
            hs.put("meta",new Constance("msg",404));
        }


        return objectMapper.writeValueAsString(hs);
    }



    //查询所有挂号级别信息 参数 无
    @RequestMapping(value = "/findAllRegistlevel",method = RequestMethod.POST)
    public String findAllRegistlevel(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        List<Registlevel> list = registlevelService.selectAll();
        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();

        //判断是否查询成功
        if(list.size()>0){
            hs.put("meta",new Constance("msg",200));
            hs.put("list",list);
        }else{
            hs.put("meta",new Constance("msg",404));
        }
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/findRegByPage",method = RequestMethod.GET)
    public String findRegByPage(@RequestParam Map<String,String> map) throws JsonProcessingException {
        //获取参数
        String query = map.get("query");
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<Registlevel> regList = registlevelService.findAllReg(query);
        PageInfo<Registlevel> info = new PageInfo<>(regList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("regList",regList);
        if (regList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取成功", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteRegById",method = RequestMethod.DELETE)
    public String deleteReg(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(registlevelService.deleteRegById(id)){
            hs.put("meta", new Constance("删除成功", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteRegsById", method = RequestMethod.POST)
    public String deleteRegs(@RequestBody Map<String,List<Integer>>ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Integer num = registlevelService.deleteRegsById(ids.get("ids"));
        if(num>0){
            hs.put("meta", new Constance("删除成功！"+num+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/insertReg",method = RequestMethod.POST)
    public String addReg(@RequestBody Registlevel registlevel)throws JsonProcessingException{
        HashMap<String, Object> hs = new HashMap<>();
        if(registlevelService.insertReg(registlevel)){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value="/findRegById",method = RequestMethod.GET)
    public String findRegById(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Registlevel registlevel = registlevelService.findRegById(id);
        if(registlevel!=null){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        hs.put("data", registlevel);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updateReg", method = RequestMethod.PUT)
    public String updateReg(@RequestBody Registlevel registlevel) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(registlevelService.updateReg(registlevel)){
            hs.put("meta", new Constance("修改成功", 200));
        }else{
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/findregistelevel",method = RequestMethod.GET)
    public List<Registlevel> findAllReg(){
        return  registlevelService.quetyAll("");
    }

}
